from django.db.models import aggregates, Sum

from goods_info.models import GoodsInventoryModel


def Get_inventory(goods_id,warehouse_id=0):

    '''
    获取某个货品的当前库存，如果warehouse_id=0 查询所有仓库库存
    :param goods_id:
    :param warehouse_id:
    :return:
    '''
    sum_inventory=0
    if warehouse_id==0:
       result= GoodsInventoryModel.objects.filter(goods_id=goods_id).aggregate(sum=Sum('cur_inventory'))
    else:
       result= GoodsInventoryModel.objects.filter(goods_id=goods_id,warehouse__id=warehouse_id).aggregate(sum=Sum('cur_inventory'))

    if result['sum'] and result['sum']!='None':
        sum_inventory=result['sum']

    return sum_inventory